package net.adetech.web.utils;

import net.adetech.utils.spring.PageBean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * Created by sunfeihu on 2016/10/21.
 */
public class UtilsUI {


    public static String tip(String message) {
        JSONObject rs = new JSONObject();
        rs.put("tip", message);
        return rs.toJSONString();
    }

    public static String data(Object data) {
        JSONObject obj = new JSONObject();
        obj.put("data", data);
        return obj.toJSONString();
    }


    public static String ok() {
        JSONObject rs = new JSONObject();
        rs.put("status", "ok");
        return rs.toJSONString();
    }


    public static String fail() {
        JSONObject rs = new JSONObject();
        rs.put("status", "fail");
        return rs.toJSONString();
    }

    public static String status(String status, Object data) {
        JSONObject rs = new JSONObject();
        rs.put("status", status);
        rs.put("data", data);
        return rs.toJSONString();
    }

    public static String status(String status) {
        JSONObject rs = new JSONObject();
        rs.put("status", status);
        return rs.toJSONString();
    }

    public static String datagrid() {
        JSONObject rs = new JSONObject();
        rs.put("totalRows", 0);
        rs.put("totalPages", 0);
        rs.put("nowPage", 0);
        rs.put("pageSize", 0);
        rs.put("data", new JSONArray());
        return rs.toJSONString();
    }

    public static String datagrid(PageBean<?> pageBean) {
        JSONObject rs = new JSONObject();
        if (null != pageBean) {
            rs.put("totalRows", pageBean.getTotalRows());
            rs.put("totalPages", pageBean.getTotalPages());
            rs.put("nowPage", pageBean.getNowPage());
            rs.put("pageSize", pageBean.getPageSize());
            rs.put("data", pageBean.getResultList());
        } else {
            rs.put("totalRows", 0);
            rs.put("totalPages", 0);
            rs.put("nowPage", 0);
            rs.put("pageSize", 0);
            rs.put("data", new JSONArray());
        }
        return rs.toJSONString();
    }

    public static String datagrid(List<?> list) {
        JSONObject rs = new JSONObject();
        if (null != list) {
            rs.put("totalRows", list.size());
            rs.put("totalPages", 1);
            rs.put("nowPage", 1);
            rs.put("pageSize", 10);
            rs.put("data", list);
        } else {
            rs.put("totalRows", 0);
            rs.put("totalPages", 0);
            rs.put("nowPage", 0);
            rs.put("pageSize", 0);
            rs.put("data", new JSONArray());
        }
        return rs.toJSONString();
    }


    public static String datagrid(JSONArray list) {
        JSONObject rs = new JSONObject();
        if (null != list) {
            rs.put("totalRows", list.size());
            rs.put("totalPages", 1);
            rs.put("nowPage", 1);
            rs.put("pageSize", 10);
            rs.put("data", list);
        } else {
            rs.put("totalRows", 0);
            rs.put("totalPages", 0);
            rs.put("nowPage", 0);
            rs.put("pageSize", 0);
            rs.put("data", new JSONArray());
        }
        return rs.toJSONString();
    }

}